package com.zz.service.impl;

import com.zz.entity.Activity;
import com.zz.entity.ESActivity;
import com.zz.repository.ESActivityRepository;
import com.zz.service.ActivityService;
import com.zz.service.ESActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/***
 * @author 周紫
 **/
@Service
public class ESActivityServiceImpl implements ESActivityService {


    @Autowired
    private ESActivityRepository esActivityRepository;

    @Autowired
    private ActivityService activityService;

    //es添加数据
    public void addToES(String code) {
        ESActivity esActivity = new ESActivity();
        Activity activity = activityService.getById(code);
        BeanUtils.copyProperties(activity,esActivity);
        esActivityRepository.save(esActivity);
        searchById(esActivity.getId().toString());
    }

    //es修改数据
    public String esUpdate(ESActivity esActivity){
        // 注意，es是根据id修改信息的，id必须是es里面的id,而非mysql数据库里面的id
        ESActivity activity=searchById(esActivity.getId().toString());
        if (activity!=null){
            esActivityRepository.save(esActivity);
            return "ok";
        }
        return "error";
    }
    public void esDelete(ESActivity esActivity){
        // 注意，es是根据id删除信息的，id必须是es里面的id,而非mysql数据库里面的id
        esActivityRepository.deleteById(esActivity.getId().toString());
    }


    public ESActivity searchById(String id) {
        Optional<ESActivity> esActivity = esActivityRepository.findById(id);
        return esActivity.get();
    }


}
